function [f,g]=create_liberal(data_congress,data_state,dw_congress,dw_state,dws);

    %%% This function creates the current and lagged shares of liberal
    %%% (left to House DW median) members of congress for each congress-state, 
    %%% for the data to be used for the regression. data_ vectors are inputs
    %%% for the regression, dw_ are inputs for the scores. The idea is to create
    %%% a matrix in which each entry has the proportion of liberals by congress
    %%% state and then reassign this to the regression data vector to create the 
    %%% liberal and liberal lag variables.;

S=length(data_congress);

cong_u=unique(dw_congress);
state_u=unique(dw_state);

dw_medians=zeros(1,length(cong_u));
liberal=zeros(length(state_u),length(cong_u));
libs_alt=zeros(S,1);
libslag_alt=zeros(S,1);


for i=1:length(cong_u);
    dw_medians(i)=median(dws(dw_congress==cong_u(i)));
    
    for j=1:length(state_u);

        dw_cs=dws(dw_congress==cong_u(i) & dw_state==state_u(j) );
        liberal(j,i)=mean(dw_cs<dw_medians(i));
        
        
    end
end


for i=1:S;
    for k=1:length(state_u);
        for j=2:length(cong_u)
            if data_congress(i)==cong_u(j) && data_state(i)==state_u(k);
                libs_alt(i)=liberal(k,j);
                libslag_alt(i)=liberal(k,j-1);
            end
        end
    end
end
f=libs_alt;
g=libslag_alt;
